import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

class SocketClient extends JFrame implements Runnable, ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JpegPanel panel;
	JTextField ipField;
	JButton connectButton;
	JButton state_button ;
	Socket socket = null;
	PrintWriter out = null;
	BufferedInputStream in = null;
	MJPEGParser parser;
	String host;

	public SocketClient() {
		this.setPreferredSize(new Dimension(640, 480));
		panel = new JpegPanel();
		panel.setSize(640, 480);
		panel.setImage(Toolkit.getDefaultToolkit().getImage("jojo.jpeg"));
		ipField = new JTextField();
		ipField.setSize(320, 12);
		connectButton = new JButton("connect");
		connectButton.addActionListener(this);
		state_button = new JButton("histogramm");
		state_button.addActionListener(this);
		state_button.setEnabled(false);
		this.setBackground(Color.WHITE);
		this.setLayout(new BorderLayout(3, 3));
		this.add("Center", panel);
		JPanel inputPanel = new JPanel();
		inputPanel.setLayout(new GridLayout(1, 3, 10, 10));
		inputPanel.add(ipField);
		inputPanel.add(connectButton);
		inputPanel.add(state_button);
		this.add("South", inputPanel);
		this.pack();
		this.setVisible(true);
		this.repaint();
		parser = new MJPEGParser(in, panel);
	} // End Constructor

	public boolean listenSocket(String ip) {
		try {
			socket = new Socket(ip, 9745);
			out = new PrintWriter(socket.getOutputStream(), true);
			in = new BufferedInputStream(socket.getInputStream());
			parser = new MJPEGParser(in, panel);
			return true;
		} catch (UnknownHostException e) {
			ipField.setText("Unknown host: " + ip);
			return false;
		} catch (IOException e) {
			ipField.setText("IO error ont: " + ip);
			return false;
		}
	}

	public static void main(String[] args) {
		SocketClient frame = new SocketClient();
		frame.setTitle("Client Program");
		WindowListener l = new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		};

		frame.addWindowListener(l);
		frame.pack();
		frame.setVisible(true);
		// frame.listenSocket();
		Thread frameThread = new Thread(frame);
		frameThread.start();
	}

	@Override
	public void run() {
		while (true) {
			try {
				if (socket != null && this.parser != null
						&& socket.isConnected()) {
					if (this.parser.parse()) {
						this.repaint();
					}
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == connectButton ||e.getSource() == ipField ) {
			try {
				if (connectButton.getText().equals("connect")) {
					if (socket != null && socket.isConnected()) {

						socket.close();
						parser = null ;
						if (this.listenSocket(ipField.getText())) {
							connectButton.setText("disconnect");
							state_button.setEnabled(true);
						}
					} else {
						if (this.listenSocket(ipField.getText())) {
							connectButton.setText("disconnect");
							state_button.setEnabled(true);
						}
					}

				} else {
					socket.close();
					connectButton.setText("connect");
				}
				this.repaint();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		if (e.getSource() == state_button){
			if(state_button.getText().equals("histogramm")){
				state_button.setText("vision");
			}else{
				state_button.setText("histogramm");
			}
			this.repaint();
		}

	}
}